package UiUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;



public class ReadCsv {
	
	public static String [] Columnname;
	public static int TestRecordFound=0;
	public static Properties PropKeys;
public static void main(String [] a)
{
	//new ReadCsv().validateExcel(a[0]);
	
}
public Properties readFormId(String PropsFilePath)
{
	String key,keyValue;
	Properties mapProperties = new Properties();
	FileInputStream in = null;
	try {
		in = new FileInputStream(new File(PropsFilePath));
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	try {
		mapProperties.load(in);
		
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return mapProperties;
	
}
public String TrimValue(String trimValue)
{

	return trimValue.trim();
	
}
public String compare(String UiValue,String xlValue,String Columnname)
{
	
	UiValue=TrimValue(UiValue);
	xlValue=TrimValue(xlValue);
	if(UiValue=="" && xlValue=="")
	{
		System.out.println("null in UI and xl");
		return "null in UI and xl\n";
	}
	else if(UiValue.equals(xlValue))
	{
		System.out.println(Columnname+" - Passed");
		return Columnname+" - Passed\n";
	}
	else
	{
		System.out.println(Columnname+" - Failed");
		return Columnname+" - Failed\n";
	}
}


public void validateTc(String xlfilepath,String TestCaseName,String Propertiespath )
{
	try{
		
	FileInputStream file = new FileInputStream(new File(xlfilepath));
	//Create Workbook instance holding reference to .xlsx file
    XSSFWorkbook workbook = new XSSFWorkbook(file);
    String excelvalue = null,columnkey=null,columnkeyname=null,xlValue=null,FormValue=null;
    ss xlsx = new ss();
    BrowserSetup br=new BrowserSetup();
    Hashtable columnshashtable = new Hashtable();

    //Get first/desired sheet from the workbook
    XSSFSheet sheet = workbook.getSheetAt(0);
    xlsx.readxlsxcolumn(sheet);
	xlsx.sheet = sheet;
    System.out.println(sheet.getSheetName());
  //Iterate through each rows one by one
    Iterator<Row> rowIterator = sheet.iterator();
    rowIterator.next();
    while (rowIterator.hasNext()) 
    {int i1 = 0,TcMatch=0;
        Row row = rowIterator.next();
       
        int lastColumn = Math.max(row.getLastCellNum(), 0);
        //For each row, iterate through all the columns
        Iterator<Cell> cellIterator = row.cellIterator();
         
       
            
            for (int cn = 0; cn < lastColumn; cn++) {
            Cell c = row
					.getCell(cn, Row.RETURN_BLANK_AS_NULL);
			if (c == null) {
				// The spreadsheet is empty in this cell
				excelvalue = " ";
				 //System.out.println(xlsx.columnVector.elementAt(i1)+"  "+excelvalue+" "+i1+"no\t"+cn+lastColumn);
				columnshashtable.put(
						xlsx.columnVector.elementAt(i1++),
						excelvalue);
			} else {
				// Do something useful with the cell's
				// contents
				c.setCellType(1);
				excelvalue = c.getRichStringCellValue()
						.getString();
				//System.out.println(xlsx.columnVector.elementAt(i1)+"  "+excelvalue+" "+cn+"--last"+lastColumn);
				if(((String) xlsx.columnVector.elementAt(i1)).equalsIgnoreCase("TestCaseName"))
	            {
					if(TestCaseName.equals(excelvalue))
					{
						TcMatch=1;
						TestRecordFound=1;
					}
	            	
	            }
				columnshashtable.put(
						xlsx.columnVector.elementAt(i1++),
						excelvalue);
				
			}
            
        }
            if(TcMatch==1)
            {
            	
            	PropKeys=readFormId(Propertiespath);
            	Enumeration enumKeys = PropKeys.keys();
            	while (enumKeys.hasMoreElements()) {
            		columnkey = (String) enumKeys.nextElement();
            		columnkeyname = PropKeys.getProperty(columnkey);
            		xlValue=(String) columnshashtable.get(columnkeyname);
            		
            		FormValue=BrowserSetup.getInfo(columnkey);
            		System.out.println(FormValue+"\t"+xlValue+"\t"+columnkeyname+"\t"+columnkey);
            		 BrowserSetup.logMessage+=compare(FormValue, xlValue, columnkeyname);
            		
            	}
            }
            
        System.out.println("");
    }
    
    file.close();
} 
catch (Exception e) 
{
    e.printStackTrace();
}
	
}
}
